using System;
using System.Web.UI;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace Jfc.DnnMarketingTools.MemberInvite
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Settings class manages User Settings
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    partial class UserSettings : PortalModuleBase
    {

        #region Base Method Implementations

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadSettings loads the settings from the Database and displays them
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            try
            {
                if (Page.IsPostBack == false)
                {
                    MemberInviteSettingsController controller = new MemberInviteSettingsController();
                    MemberInviteSettingsInfo memberInviteSettings = controller.GetMemberInviteSettings(PortalId, UserId);

                    txtSubjectTemplate.Text = memberInviteSettings.SubjectTemplate;
                    txtBodyTemplate.Text = memberInviteSettings.BodyTemplate;
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// UpdateSettings saves the modified settings to the Database
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        protected void cmdUpdate_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                MemberInviteSettingsController controller = new MemberInviteSettingsController();
                MemberInviteSettingsInfo memberInviteSettings = new MemberInviteSettingsInfo();

                memberInviteSettings.PortalId = PortalId;
                memberInviteSettings.UserId = UserId;
                memberInviteSettings.SubjectTemplate = txtSubjectTemplate.Text;
                memberInviteSettings.BodyTemplate = txtBodyTemplate.Text;

                controller.SetMemberInviteSettings(memberInviteSettings);

                //refresh cache
#pragma warning disable 0618
                SynchronizeModule();
                //ModuleController.SynchronizeModule(ModuleId);
#pragma warning restore

                //Redirect back to the portal home page
                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void cmdCancel_Click(System.Object sender, System.EventArgs e)
        {
            try
            {
                this.Response.Redirect(Globals.NavigateURL(this.TabId), true);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

    }
}


